package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.shopping.dto.GoodsDto;
import com.cskaoyan.mall.dto.ItemDto;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.mall.dto.ProductDto;
import com.cskaoyan.shopping.constants.ShoppingRetCode;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.ItemDesc;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.form.PageRequest;
import com.cskaoyan.shopping.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Set;

@Service
public class IProductServiceImpl implements IProductService {

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ItemDescMapper itemDescMapper;

    @Autowired
    ProductConverter productConverter;
    @Override
    public ProductDetailDto getProductDetail(Long productId) {
        Item item = itemMapper.selectByPrimaryKey(productId);
        ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(productId);

        if (item == null) {
            //未找到productId对应的商品
            throw new BizException(ShoppingRetCode.PRODUCT_NOT_EXISTS.getCode(),ShoppingRetCode.PRODUCT_NOT_EXISTS.getMessage());
        }
        ProductDetailDto productDetailDto = productConverter.toProductDetailDto(item, itemDesc);

        return productDetailDto;
    }

    @Override
    public AllProductResponse getAllProduct(PageRequest pageInfo) {
        return null;
    }

    @Override
    public Set<PanelDto> getRecommendGoods() {
        return null;
    }

    @Override
    public List<ItemDto> getAllItems() {
        return null;
    }

    @Override
    public GoodsDto queryProductList(Integer page, Integer size, String sort, Integer priceGt, Integer priceLte, Long cid) {
        //分页操作
        PageHelper.startPage(page,size);

        Example example = new Example(Item.class);

        //排列顺序
        if (sort != null && !"".equals(sort)) {
            if (Integer.parseInt(sort) > 0) {
                sort = "asc";
            } else {
                sort = "desc";
            }

            String priceSort = "price "+sort;
            example.setOrderByClause(priceSort);
        }

        Example.Criteria criteria = example.createCriteria();
        if (cid != null) {
            criteria.andEqualTo("cid",cid);
        }
        if (priceLte != null) {
            criteria.andLessThanOrEqualTo("price",priceLte);
        }
        if (priceGt != null) {
            criteria.andGreaterThanOrEqualTo("price",priceGt);
        }

        List<Item> items = itemMapper.selectByExample(example);
        List<ProductDto> productDtos = productConverter.items2Dto(items);
        GoodsDto goodsDto = new GoodsDto();
        goodsDto.setData(productDtos);
        PageInfo PageInfo = new PageInfo(items);
        goodsDto.setTotal(PageInfo.getTotal());

        return goodsDto;
    }
}
